package com.two.screen.water.balance.print.android_print.bluetooth

import com.android.print.sdk.PrinterConstants
import com.two.screen.water.balance.net.api.DataApi
import com.two.screen.water.balance.print.all.Table
import com.two.screen.water.balance.print.all.USBPrinterAll
import com.two.screen.water.balance.print.all.ZXingUtils
import com.two.screen.water.balance.print.android_print.IPrinterOpertion
import com.two.screen.water.balance.print.bean.PrintConnectBean
import com.two.screen.water.balance.print.bean.PrintConnectSaleReportBean
import com.two.screen.water.balance.print.bean.PrintOrderBean
import com.two.screen.water.balance.print.bean.PrintServerCollectOrderBean
import com.two.screen.water.balance.utils.CommUtils
import com.two.screen.water.balance.utils.Params
import com.two.screen.water.balance.utils.SPUtils
import com.two.screen.water.balance.utils.isNotAvailable

object BluetoothPrinter {
    const val CONNECT_DEVICE = 1 //选择设备
    const val ENABLE_BT = 2 //启动蓝牙

    const val EXTRA_DEVICE_ADDRESS = "device_address"
    const val EXTRA_RE_PAIR = "re_pair"
    const val EXTRA_DEVICE_NAME = "device_name"

    //是否已经建立了连接
    var isConnected = false
    var isConnecting = false //用来判断是否正在连接
    var myOpertion: IPrinterOpertion? = null

    var mBluetoothPrinterName: String? = SPUtils.getString(Params.mBluetoothPrinterName)
    var mBluetoothPrinterMac: String? = SPUtils.getString(Params.mBluetoothPrinterMac)


    fun printTest() {
        var times = DataApi.mBillPrintTimesArray[DataApi.mBillPrintTimesIndex].toInt()
        myOpertion?.printer?.init()
        for (time in 1..times) {
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.ALIGN, 1);//居中
            myOpertion?.printer?.setFont(0, 0, 1, 0);//加粗
            myOpertion?.printer?.printText("测试打印")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.setFont(0, 0, 0, 0);//取消加粗
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.ALIGN, 0);//居左
            myOpertion?.printer?.printText("一二三四五六七八九十")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("==============================================")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 3);// 换行
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.ALIGN, 1);//居中
            myOpertion?.printer?.printImage(ZXingUtils.createQRCodeBitmap("测试打印二维码", 200))//二维码
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.ALIGN, 0);//居左
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 3);// 换行
            myOpertion?.printer?.printText("12345678910..")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 3);// 换行
        }
    }

    fun printOrder(data: PrintOrderBean?) {
        var times = DataApi.mBillPrintTimesArray[DataApi.mBillPrintTimesIndex].toInt()
        for (time in 1..times) {
            USBPrinterAll.usbPrinter?.init()
            myOpertion?.printer?.setFont(1, 1, 0, 0);//加粗+放大一倍
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.ALIGN, 1);//居中
            myOpertion?.printer?.printText(data?.title)
            myOpertion?.printer?.setFont(0, 0, 0, 0);//正常大小
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.ALIGN, 0);//居左
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 2);// 换行

            myOpertion?.printer?.printText("市场名称：${DataApi.mMarketName.isNotAvailable()}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("商户名称：${DataApi.mShopName.isNotAvailable()}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("档口号：${DataApi.mShopStallName.isNotAvailable()}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("订单编号：${data?.orderSn.isNotAvailable()}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("收银时间：${data?.collectionTime.isNotAvailable()}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("操作人：${data?.operationName.isNotAvailable()}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText(data?.orderPriceInfo.isNotAvailable())
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("支付信息：${data?.payInfo.isNotAvailable()}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行


            if (data?.productList != null && data.productList!!.isNotEmpty()) {
                myOpertion?.printer?.printText("-------------------------------")
                myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
                myOpertion?.printer?.printText("品名")
                myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
                val tableHead = "数量;单价;金额"
                var table: Table? = Table(tableHead, ";", intArrayOf(14, 10, 14))
                table?.addRow("-------------------------------")
                data.productList?.indices?.forEach {
                    var bean = data.productList!![it]
                    table?.addRow(bean.name ?: "商品")
                    table?.addRow("${bean.weight ?: "0.000"};${bean.price ?: "0.00"};${bean.subPrice ?: "0.00"}")
                }
                myOpertion?.printer?.printText(table?.tableText)
                myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
                myOpertion?.printer?.printText("-------------------------------")
                myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            }
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.ALIGN, 0);//居左
            myOpertion?.printer?.printText(data?.orderTotal.isNotAvailable())
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.ALIGN, 1);//居中
            myOpertion?.printer?.setFont(0, 0, 1, 0);//加粗
            if (data?.qrCode != null && data.qrCode!!.isNotBlank()) {
                myOpertion?.printer?.printText(data.qrCodeTipsTop.isNotAvailable())
                myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 3);// 换行
                myOpertion?.printer?.printImage(ZXingUtils.createQRCodeBitmap(data.qrCode, 200))//二维码
                myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 3);// 换行
            }
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.ALIGN, 1);//居中
            myOpertion?.printer?.printText(data?.qrCodeTipsBottom.isNotAvailable())
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.ALIGN, 0);//居左
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 5);// 换行
        }
    }

    fun printConnectStatics(data: PrintConnectBean?) {
        USBPrinterAll.usbPrinter?.init()
        var times = DataApi.mBillPrintTimesArray[DataApi.mBillPrintTimesIndex].toInt()
        for (time in 1..times) {
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.ALIGN, 1);//居中
            myOpertion?.printer?.setFont(1, 1, 0, 0);//加粗
            myOpertion?.printer?.printText(data?.title)
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 2);// 换行
            myOpertion?.printer?.setFont(0, 0, 0, 0);//取消加粗
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.ALIGN, 0);//居左

            myOpertion?.printer?.printText("收银员：${data?.operationName.isNotAvailable()}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("起始时间：${data?.startTime.isNotAvailable()}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("结束时间：${data?.endTime.isNotAvailable()}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("-------------------------------")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行

            myOpertion?.printer?.printText("总单数：　　　　${data?.billTotal.isNotAvailable()}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("门店：　　　　　${data?.billShop.isNotAvailable()}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("退货：　　　　　${data?.billReturn.isNotAvailable()}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("反结账：　　　　${data?.billReverse.isNotAvailable()}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("-------------------------------")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行

            myOpertion?.printer?.printText("总实收金额：　　${data?.realTotalPrice.isNotAvailable()}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("现金实收：　　　${data?.realCashPrice.isNotAvailable()}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("微会员实收：　　${data?.realWetchVipPrice.isNotAvailable()}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("聚合支付实收：　${data?.realJuHePrice.isNotAvailable()}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("扫码收款实收：　${data?.realScanQrPrice.isNotAvailable()}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("-------------------------------")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行

            myOpertion?.printer?.printText("总销售额：　　　${data?.saleTotalPrice.isNotAvailable()}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("现金：　　　　　${data?.saleCashPrice.isNotAvailable()}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("微会员：　　　　${data?.saleWetchVipPrice.isNotAvailable()}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("聚合支付：　　　${data?.saleJuHePrice.isNotAvailable()}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("扫码收款：　　　${data?.saleScanQrPrice.isNotAvailable()}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("-------------------------------")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 7);// 换行
        }
    }

    fun printConnectSaleReport(data: PrintConnectSaleReportBean?) {
        USBPrinterAll.usbPrinter?.init()
        var times = DataApi.mBillPrintTimesArray[DataApi.mBillPrintTimesIndex].toInt()
        for (time in 1..times) {
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.ALIGN, 1);//居中
            myOpertion?.printer?.setFont(1, 1, 0, 0);//加粗
            myOpertion?.printer?.printText(data?.title)
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 2);// 换行
            myOpertion?.printer?.setFont(0, 0, 0, 0);//取消加粗
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.ALIGN, 0);//居左

            myOpertion?.printer?.printText("收银员：${data?.operationName.isNotAvailable()}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("统计时间：${CommUtils.toYMD_HMS(System.currentTimeMillis() / 1000)}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("起始 ${data?.startTime.isNotAvailable()}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("结束 ${data?.endTime.isNotAvailable()}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行


            if (data?.productList != null && data.productList!!.isNotEmpty()) {
                myOpertion?.printer?.printText("-------------------------------")
                myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
                myOpertion?.printer?.printText("品名")
                myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行

                val tableHead = "数量;单价;小计"
                var table: Table? = Table(tableHead, ";", intArrayOf(14, 10, 14))
                table?.addRow("-------------------------------")
                data.productList?.indices?.forEach {
                    var bean = data.productList!![it]
                    table?.addRow(bean.name ?: "商品")
                    table?.addRow("${bean.weight ?: "0.000"};${bean.price ?: "0.00"};${bean.subPrice ?: "0.00"}")
                }
                myOpertion?.printer?.printText(table?.tableText)
                myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
                myOpertion?.printer?.printText("-------------------------------")
                myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            }
            myOpertion?.printer?.printText(data?.total.isNotAvailable())
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 7);// 换行
        }
    }

    fun closeBluetoothPrinter() {
        try {
            myOpertion?.close()
            myOpertion = null
        } catch (e: Exception) {
            d("closeBluetoothPrinter->error:$e")
        }
    }

    private fun d(msg: String) {
        CommUtils.println("BluetoothPrinter->", msg)
    }

    fun printServerCollectOrder(printBean: PrintServerCollectOrderBean) {
        USBPrinterAll.usbPrinter?.init()
        var times = DataApi.mBillPrintTimesArray[DataApi.mBillPrintTimesIndex].toInt()
        for (time in 1..times) {
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.ALIGN, 1);//居中
            myOpertion?.printer?.setFont(1, 1, 0, 0);//加粗
            myOpertion?.printer?.printText(printBean.title)
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 2);// 换行
            myOpertion?.printer?.setFont(0, 0, 0, 0);//取消加粗
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.ALIGN, 0);//居左

            myOpertion?.printer?.printText("${printBean.shopName}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("${printBean.startTime}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("${printBean.endTime}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("-------------------------------")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行

            myOpertion?.printer?.printText("${printBean.totalPrice}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("${printBean.findZeroPrice}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("${printBean.giftPrice}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("${printBean.realPrice}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("${printBean.orderNumbers}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("-------------------------------")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行

            myOpertion?.printer?.printText("${printBean.cashPrice}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("${printBean.miniProgramPrice}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("${printBean.vipPirce}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("${printBean.juhePrice}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行
            myOpertion?.printer?.printText("${printBean.scanQrPrice}")
            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);// 换行

            myOpertion?.printer?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 7);// 换行
        }
    }
}